<template>
  <a-modal
    title="导出表资源"
    :visible="isShow"
    @ok="handleSubmit"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @cancel="handleCancel"
    width="700px"
    :destroyOnClose="true"
  >
    <div v-if="status === 0" class="single-search-item">
      <div class="item-label">选择日期：</div>
      <div class="item-content">
        <a-date-picker
          style="width:400px;"
          showTime
          format="YYYY-MM-DD"
          @change="onChangetime"
        />
      </div>
    </div>
    <p v-if="status === 4" class="p1">{{ downLoadDate }}号没有资源！</p>
    <p v-if="status === 2" class="p1">
      <span>{{ downLoadDate }}</span
      >号 资源下载中……
    </p>
  </a-modal>
</template>
<script>
import { page } from "@/api";
import config from "@/config";
import moment from "moment";
export default {
  data() {
    return {
      form: this.$form.createForm(this),
      confirmLoading: false,
      isShow: false,
      title: "编辑字段",
      isEdit: true,
      outObj: {},
      // diffDay: 0,
      status: 0,
      filePath: "",
      downLoadDate: "",
      noFile: false
    };
  },
  mounted() {},
  methods: {
    show(obj) {
      this.confirmLoading = false;
      this.startTime = "";
      // this.status = 1;
      this.clickFlag = false;
      this.isShow = true;
      this.outObj = obj;
      //判断是否打包完成
      this.onDownLoad();
    },
    handleCancel() {
      this.isShow = false;
    },
    onChangetime(value, dateString) {
      this.downLoadDate = dateString;
      this.startTime = moment(dateString).format("YYYYMMDD");
      this.endTime = moment(dateString).format("YYYYMMDD");
      // this.diffDay = this.datedifference(
      //   moment(this.startTime).format("YYYY-MM-DD"),
      //   moment(this.endTime).format("YYYY-MM-DD")
      // );
      // debugger;
    },
    //两个时间相差天数
    datedifference(sDate1, sDate2) {
      //sDate1和sDate2是2006-12-18格式
      var dateSpan, tempDate, iDays;
      sDate1 = Date.parse(sDate1);
      sDate2 = Date.parse(sDate2);
      dateSpan = sDate2 - sDate1;
      dateSpan = Math.abs(dateSpan);
      iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
      return iDays;
    },
    handleSubmit(e) {
      e.preventDefault();
      if (this.filePath === "") {
        if (this.startTime === undefined || this.startTime === "") {
          this.$message.warning("请选择时间！");
          return;
        }
        this.confirmLoading = true;
        let params = Object.assign(this.outObj, {
          startTime: this.startTime,
          endTime: this.endTime
        });
        // debugger;
        // 后端打包zip接口
        page
          .sourceExport(params)
          .then(res => {
            if (res.data.code === -1) {
              this.$message.warning(res.data.message);
            } else {
              this.filePath = res.data.data.filePath;
              this.status = 2;
              // 每10分钟查询是否可以下载资源
              setInterval(() => {
                this.onDownLoad();
              }, 10 * 60 * 1000);
            }
            this.confirmLoading = false;
          })
          .catch(() => {
            this.isShow = false;
            this.confirmLoading = false;
            this.$message.warning("导出失败");
          });
      } else {
        this.onDownLoad();
        this.isShow = false;
      }
    },
    initStatus() {
      this.status = 0;
      this.filePath = "";
      this.downLoadDate = "";
      this.noFile = false;
    },
    onDownLoad() {
      debugger;
      if (this.filePath !== "") {
        if (this.noFile) {
          this.initStatus();
          return;
        }
        page.zipResult({ zipUrl: this.filePath }).then(res => {
          if (res.data === 1) {
            //打包完毕，开始下载
            const link = document.createElement("a");
            link.style.display = "none";
            link.href = config.fileUrl + this.filePath;
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
            // 下载完成，设为初始状态,下载下一个
            this.initStatus();
          }
          if (res.data === 2) {
            // 打包中
            this.status = 2;
          }
          if (res.data === 3) {
            // 下载失败
            this.$message.warning("资源下载失败！");
            this.initStatus();
          }
          if (res.data === 4) {
            // 没有文件
            this.status = 4;
            this.noFile = true;
          }
        });
      }
    }
  }
};
</script>
<style scoped>
.p1 {
  text-align: center;
  font-size: 20px;
}
.single-search-item {
  height: 100px;
  width: 380px;
  padding: 20px 10px;
}
.item-label {
  float: left;
  width: 121px;
  text-align: right;
  line-height: 30px;
}
.item-content {
  float: left;
  width: 198px;
}
</style>
